[AI人工智能] python 錯誤訊息及解決方式


Posted by mike-hsieh on 2024-03-20

使用 pytorch 建立神經網路和模型時,發生了錯誤,這邊紀錄一下。

1. RuntimeError: mat1 and mat2 must have the same dtype

  • 表示兩種數字的精度有差異,就要看報錯的那一個變數是什麼型別,通常就是轉為 float32 就可以了。
# 解決方式:
self.features = features.astype('float32')
self.labels = labels.astype('float32')


2. PyTorch ValueError: Target size (torch.Size([64])) must be the same as input size (torch.Size([15]))

  • 表示輸入的特徵數量和模型設定的特徵數量不符。
# 解決方式: 通常可以動態傳入,或是確定好兩者數量一樣,例如:
X_train.shape[0]


3. TypeError: can't convert np.ndarray of type numpy.object_

  • 表示數字轉換失敗。
# 解決方式(基本同1):
self.features = features.astype('float32')
self.labels = labels.astype('float32')


4. too many values to unpack

  • 表示回傳的值數量少於接收值。
# 例如: A方法回傳1個數字,但是接收時寫成
rtnVal1, rtnVal2 = A()

# 解決方式:
# 確認好回傳的參數是多少,和接收端一致。

#python-pytorch-error-solve







Related Posts

[FE102] 實戰演練:JavaScript

[FE102] 實戰演練:JavaScript

[Golang] Tags

[Golang] Tags

CSS transform property

CSS transform property


Comments